package com.yulan.bankback.controller.bank;


import com.yulan.bankback.aop.Authorization;
import com.yulan.bankback.entity.result.RetResponse;
import com.yulan.bankback.entity.result.RetResult;
import com.yulan.bankback.entity.vo.MsgVo;
import com.yulan.bankback.service.bank.MsgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;


@RestController
@RequestMapping("/msg")
public class MsgController {

    @Autowired
    private MsgService msgService;

    @Authorization({"admin", "bank_user"})
    @GetMapping("/chat/{uid}")
    public RetResult getUserChatVo(@PathVariable("uid") String uid){
        if(StringUtils.isBlank(uid)) return RetResponse.makeErrRsp("uid不能为空");
        MsgVo vo = msgService.getMsgVo(uid);
        return vo == null ? RetResponse.makeErrRsp("没有该用户") : RetResponse.makeOKRsp(vo);
    }

    @Authorization({"admin", "bank_user"})
    @PostMapping("/chat/add_msg")
    public RetResult addUserChat(String srcUid, String destUid,
                                 String msg, String type, String msgDate) throws ParseException {
        if(StringUtils.isBlank(srcUid)) return RetResponse.makeErrRsp("srcUid不能为空");
        if(StringUtils.isBlank(destUid)) return RetResponse.makeErrRsp("destUid不能为空");
        if(StringUtils.isBlank(msg)) return RetResponse.makeErrRsp("发送的消息不能为空");
        if(StringUtils.isBlank(type)) return RetResponse.makeErrRsp("消息类型不能为空");
        if(StringUtils.isBlank(msgDate)) return RetResponse.makeErrRsp("消息时间不能为空");
        msgDate = msgDate.replaceAll("/", "-");
        String res = msgService.addMsg(srcUid, destUid, msg, type, msgDate);
        return res.equals("success") ? RetResponse.makeOKRspMsg("发送成功") : RetResponse.makeErrRsp(res);

    }

    @Authorization({"admin", "bank_user"})
    @PostMapping("/chat/get_money")
    public RetResult getMoneyMsg(String uid, String msgId){
        if(StringUtils.isBlank(uid)) return RetResponse.makeErrRsp("uid不能为空");
        if(StringUtils.isBlank(msgId)) return RetResponse.makeErrRsp("msgId不能为空");
        String res = msgService.getMoneyMsg(uid, msgId);
        return res.equals("success") ? RetResponse.makeOKRsp() : RetResponse.makeErrRsp(res);
    }

    @Authorization({"admin", "bank_user"})
    @PostMapping("/chat/send_money")
    public RetResult getMoneyMsg(String srcUid, String destUid, String payPassword,
                                 String msg,  String msgDate) throws ParseException {
        if(StringUtils.isBlank(srcUid)) return RetResponse.makeErrRsp("srcUid不能为空");
        if(StringUtils.isBlank(destUid)) return RetResponse.makeErrRsp("destUid不能为空");
        if(StringUtils.isBlank(payPassword)) return RetResponse.makeErrRsp("支付密码不能为空");
        if(StringUtils.isBlank(msg)) return RetResponse.makeErrRsp("发送的消息不能为空");
        if(StringUtils.isBlank(msgDate)) return RetResponse.makeErrRsp("消息时间不能为空");
        msgDate = msgDate.replaceAll("/", "-");
        String res = msgService.sendMoney(srcUid, destUid, payPassword, msg, msgDate);
        //拼接余额返回给前端更新页面
        return res.matches("success.*") ? RetResponse.makeOKRsp(res.split("_")[1]) : RetResponse.makeErrRsp(res);

    }



}
